#!/bin/bash

# -- edit original file using redirection -- 
# By default, sed edit original content and prints it to standard output.
# of cause, we can use redirect operator in shell script '>' to change the origin file

# * change John to Johnny, create a new file: test_employee_2.txt
sed 's/John/Johnny/g' ./test_employee.txt > ./test_employee_2.txt

# * filter the lines that contains "Ram", write it into test_employee_2.txt
# sed -n '/Ram/ p' ./test_employee_2.txt  > ./test_employee_2.txt
# NOTE: this command will clean all content in test_employee_2.txt

# Shortcuts: 
# * cannot just using sed to edit oringal file and redirect the result to original file
# * the content will be fully covered.



# -- edit original file using '-i' sed option
# use -i option, sed will always create backup for origin file.

# - edit original file with no bakup file
sed -i 's/John/John/' ./test_employee.txt # to avoid change sample content

# - edit original file but create a bakup file with 
sed -i.bak 's/John/Johnny/' ./test_employee.txt

# - the long option version of above 
sed --in-place=bak 's/John/Johnny/' ./test_employee.txt


